Drupal.behaviors.sphDrag = function(context)
{
    var table = $('table#ueas');
    
    // Get the blocks tableDrag object.
    var tableDrag = Drupal.tableDrag.ueas;
    
    // Add a handler for when a row is swapped, update empty regions.
    tableDrag.row.prototype.onSwap = function(swappedRow)
    {
        checkEmptyRegions(table, this);
    };

    // A custom message for the blocks page specifically.
    Drupal.theme.tableDragChangedWarning = function ()
    {
        return '<div class="warning">' + Drupal.theme('tableDragChangedMarker') + ' ' +
               Drupal.t("Para actualizar la planeaci&oacute;n haz clic en el bot&oacute;n <em>Actualizar planeaci&oacute;n</em>") +
               '</div>';
    };
    
    // Add a handler so when a row is dropped, update fields dropped into new regions.
    tableDrag.onDrop = function()
    {
        dragObject = this;
        if($(dragObject.rowObject.element).prev('tr').is('.region-message'))
        {
            var regionRow = $(dragObject.rowObject.element).prev('tr').get(0);
            var regionName = regionRow.className.replace(/([^ ]+[ ]+)*region-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
            var regionField = $('select.uea-region-select', dragObject.rowObject.element);
            var weightField = $('select.uea-weight', dragObject.rowObject.element);
            var oldRegionName = weightField[0].className.replace(/([^ ]+[ ]+)*uea-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');

            if(!regionField.is('.uea-region-'+ regionName))
            {
                regionField.removeClass('uea-region-' + oldRegionName).addClass('uea-region-' + regionName);
                weightField.removeClass('uea-weight-' + oldRegionName).addClass('uea-weight-' + regionName);
                regionField.val(regionName);
            }
        }
    };

    // Add the behavior to each region select list.
    //$('select.uea-region-select:not(.blockregionselect-processed)', context).each(function()
    $('select.uea-region-select:not(.uearegionselect-processed)', context).each(function()
    {
        $(this).change(function(event)
        {
            // Make our new row and select field.
            var row = $(this).parents('tr:first');
            var select = $(this);
            tableDrag.rowObject = new tableDrag.row(row);

            // Find the correct region and insert the row as the first in the region.
            $('tr.region-message', table).each(function()
            {
                if($(this).is('.region-' + select[0].value + '-message'))
                {
                    // Add the new row and remove the old one.
                    $(this).after(row);

                    // Manually update weights and restripe.
                    tableDrag.updateFields(row.get(0));
                    tableDrag.rowObject.changed = true;
                    if(tableDrag.oldRowElement)
                    {
                        $(tableDrag.oldRowElement).removeClass('drag-previous');
                    }
                    
                    tableDrag.oldRowElement = row.get(0);
                    tableDrag.restripeTable();
                    tableDrag.rowObject.markChanged();
                    tableDrag.oldRowElement = row;
                    $(row).addClass('drag-previous');
                }
            });
            
            // Modify empty regions with added or removed fields.
            checkEmptyRegions(table, row);
            
            // Remove focus from selectbox.
            select.get(0).blur();
        });
        
//        $(this).addClass('blockregionselect-processed');
        $(this).addClass('uearegionselect-processed');
    });
    
    var checkEmptyRegions = function(table, rowObject)
    {
        $('tr.region-message', table).each(function()
        {
            // If the dragged row is in this region, but above the message row, swap it down one space.
            if ($(this).prev('tr').get(0) == rowObject.element)
            {
                // Prevent a recursion problem when using the keyboard to move rows up.
                if ((rowObject.method != 'keyboard' || rowObject.direction == 'down'))
                {
                    rowObject.swap('after', this);
                }
            }
            
            // This region has become empty
            if($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').size() == 0)
            {
                $(this).removeClass('region-populated').addClass('region-empty');
            }
            
            // This region has become populated.
            else if($(this).is('.region-empty'))
            {
                $(this).removeClass('region-empty').addClass('region-populated');
            }
        });
    };
};
